//
//  JKRFeedAuthorTopView.m
//  JKRFeedDemo
//
//  Created by Lucky on 2017/12/10.
//  Copyright © 2017年 Lucky. All rights reserved.
//

#import "JKRFeedAuthorTopView.h"

@interface JKRFeedAuthorTopView ()

@property (nonatomic, strong) YYLabel *topLable;

@property (nonatomic, strong) UIView *topLineView;

@property (nonatomic, strong) UIImageView *headerImageView;

@property (nonatomic, strong) YYLabel *userLabel;

@property (nonatomic, strong) YYLabel *statusLabel;

@property (nonatomic, strong) YYLabel *bottomLabel;

/// 聊一聊按钮
@property (nonatomic, strong) UIButton *chatButton;
/// 结束服务按钮
@property (nonatomic, strong) UIButton *finishButton;
/// 确认服务按钮
@property (nonatomic, strong) UIButton *startButton;
/// 选择大神按钮
@property (nonatomic, strong) UIButton *selectButton;
/// 不能点击的选择大神按钮
@property (nonatomic, strong) UIButton *disabledSelectButton;

@end


@implementation JKRFeedAuthorTopView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    self.backgroundColor = [UIColor whiteColor];
    @weakify(self);

    _chatButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_chatButton setTitleColor:UIColorHex(FFFFFF) forState:UIControlStateNormal];
    [_chatButton setBackgroundColor:UIColorHex(F4AD3A)];
    [_chatButton setTitle:@"聊一聊" forState:UIControlStateNormal];
    _chatButton.titleLabel.font = [UIFont systemFontOfSize:14];
    _chatButton.layer.cornerRadius = 3.f;
    [self addSubview:_chatButton];
    
    _startButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_startButton setTitleColor:UIColorHex(FFFFFF) forState:UIControlStateNormal];
    [_startButton setBackgroundColor:UIColorHex(CE0008)];
    [_startButton setTitle:@"确认服务" forState:UIControlStateNormal];
    _startButton.titleLabel.font = [UIFont systemFontOfSize:14];
    _startButton.layer.cornerRadius = 3.f;
    [self addSubview:_startButton];
    
    _finishButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_finishButton setTitleColor:UIColorHex(FFFFFF) forState:UIControlStateNormal];
    [_finishButton setBackgroundColor:UIColorHex(CE0008)];
    [_finishButton setTitle:@"结束服务" forState:UIControlStateNormal];
    _finishButton.titleLabel.font = [UIFont systemFontOfSize:14];
    _finishButton.layer.cornerRadius = 3.f;
    [self addSubview:_finishButton];
    
    _selectButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_selectButton setTitleColor:UIColorHex(FFFFFF) forState:UIControlStateNormal];
    [_selectButton setBackgroundColor:UIColorHex(CE0008)];
    [_selectButton setTitle:@"选择大神" forState:UIControlStateNormal];
    _selectButton.titleLabel.font = [UIFont systemFontOfSize:14];
    _selectButton.layer.cornerRadius = 3.f;
    [self addSubview:_selectButton];
    
    _disabledSelectButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_disabledSelectButton setTitleColor:UIColorHex(FFFFFF) forState:UIControlStateNormal];
    [_disabledSelectButton setBackgroundColor:UIColorHex(D8D8D8)];
    [_disabledSelectButton setTitle:@"选择大神" forState:UIControlStateNormal];
    _disabledSelectButton.titleLabel.font = [UIFont systemFontOfSize:14];
    _disabledSelectButton.layer.cornerRadius = 3.f;
    [self addSubview:_disabledSelectButton];
    
    _topLable = [self _creatYYLabel];
    [self addSubview:_topLable];
    
    _topLineView = [UIView new];
    _topLineView.backgroundColor = UIColorHex(E1E1E1);
    [self addSubview:_topLineView];
    
    _statusLabel = [self _creatYYLabel];
    [self addSubview:_statusLabel];
    
    _headerImageView = [UIImageView new];
    [self addSubview:_headerImageView];
    
    _userLabel = [self _creatYYLabel];
    _userLabel.highlightTapAction = ^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        [weak_self highLightClickWithText:text range:range];
    };
    [self addSubview:_userLabel];
    
    _bottomLabel = [self _creatYYLabel];
    [self addSubview:_bottomLabel];
    
    return self;
}

- (void)setLayout:(JKRFeedTopLayout *)layout {
    _layout = layout;
    self.hidden = !layout.show;
    if (!layout.show) {
        return;
    }
    @weakify(self);
    self.frame = layout.topFrame;
    self.topLable.frame = layout.topLabelFrame;
    self.topLable.textLayout = layout.topLabelLayout;
    self.topLineView.frame = layout.topLineViewFrame;
    self.headerImageView.frame = layout.headerImageViewFrame;
    [self.headerImageView setImageWithURL:layout.headerImageViewURL placeholder:nil options:kNilOptions manager:nil progress:nil transform:nil completion:^(UIImage * _Nullable image, NSURL * _Nonnull url, YYWebImageFromType from, YYWebImageStage stage, NSError * _Nullable error) {
        weak_self.headerImageView.image = [image jkr_clipCircleImageWithBorder:0.5 withColor:UIColorHex(CCCCCC)];
    }];
    self.statusLabel.frame = layout.statusLabelFrame;
    self.statusLabel.textLayout = layout.statusLabelLayout;
    self.userLabel.frame = layout.userLabelFrame;
    self.userLabel.textLayout = layout.userLabelLayout;
    self.chatButton.frame = layout.chatButtonFrame;
    self.finishButton.frame = layout.finishButtonFrame;
    self.startButton.frame = layout.startButtonFrame;
    self.selectButton.frame = layout.selectButtonFrame;
    self.disabledSelectButton.frame = layout.disabledSelectButtonFrame;
    
    self.bottomLabel.frame = layout.bottomLabelFrame;
    self.bottomLabel.textLayout = layout.bottomLabelLayout;
}
                  
- (YYLabel *)_creatYYLabel {
    YYLabel *label = [YYLabel new];
    label.ignoreCommonProperties = YES;
    label.fadeOnAsynchronouslyDisplay = YES;
    return label;
}

- (void)highLightClickWithText:(NSAttributedString *)text range:(NSRange)range {
    YYTextHighlight *highlight = [text attribute:YYTextHighlightAttributeName atIndex:range.location];
    NSDictionary *info = highlight.userInfo;
    if (info.count == 0) {
        return;
    }
    if (info[kFeedTopClickUser]) {
        [self.topView.delegate topViewDidClickUserWithUserID:info[kFeedTopClickUser]];
        return;
    }
    if (info[kFeedTopClickCandidateList]) {
        NSLog(@"大神列表");
        return;
    }
}

@end
